import Vue from 'vue'
import Router from 'vue-router'
// import Login from '@/components/login/login.vue'
// @ -> src
import Login from '../components/login/login.vue'
import Home from '../components/home/home.vue'
import Users from '../components/users/users.vue'
import Right from '../components/rights/right.vue'
import Role from '../components/rights/role.vue'
import Goodslist from '../components/goods/goodslist.vue'
import Goodsadd from '../components/goods/goodsadd.vue'
import Cateparams from '../components/goods/cateparams.vue'
import Goodscate from '../components/goods/goodscate.vue'
import Order from '../components/order/order.vue'
import Reports from '../components/reports/reports.vue'
// 引入message
import { Message } from 'element-ui'



Vue.use(Router)

const router = new Router({
  routes: [
    {
      name: 'login',
      path: '/login',
      component: Login
    },
    {
      name: 'home',
      path: '/',
      component: Home,

      children: [
        {
          name: 'users',
          path: 'users',
          component: Users
        },
        {
          name: 'right',
          path: '/rights',
          component: Right
        },
        {
          name: 'roles',
          path: '/roles',
          component: Role
        },
        {
          name: 'goods',
          path: '/goods',
          component: Goodslist,
          
        },
        {
          name: 'goodsadd',
          path: '/goodsadd',
          component: Goodsadd,
          
        },
        {
          name: 'cateparams',
          path: '/params',
          component: Cateparams,
          
        },
        {
          name:'goodscate',
          path:'/categories',
          component:Goodscate
        },
        {
          name:'order',
          path:'/orders',
          component:Order
        },
        {
          name:'reports',
          path:'/reports',
          component:Reports
        },
      ]


    }
  ]
})

// 在路由配置生效之前 统一判断 token
// 路由守卫 在路由配置生效之前
// 路由 / 导航 / 守卫
// to  要去的路由配置
// from 当前的路由配置
router.beforeEach((to, from, next) => {
  // next()  自动调用下一个中间件
  // 如果要去的是登录 》 next()
  if (to.path === '/login') {
    next()
  } else {
    //  如果不是 就要判断token
    const token = localStorage.getItem('token')
    // 如果 没有 》 回到登录
    if (!token) {
      // 提示 回到登录页
      Message.warning('请先登录')
      router.push({
        name: 'login'
      })
      return
    }
      // 如果有 》 next() 
    next()
  }
})
export default router


